package com.gdms.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gdms.entity.User;


/**
 * 过滤器
 */

public class LoginFilter implements Filter {
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		
		HttpServletRequest servletRequest = (HttpServletRequest)request;
		HttpServletResponse servletResponse = (HttpServletResponse) response;
		HttpSession session = servletRequest.getSession();
		
		String path = servletRequest.getRequestURI();
		
		System.out.println("当前路径："+path);
		User currentUser = (User)session.getAttribute("currentUser");
		if (path.contains("index.jsp") || path.contains("teacherLogin.jsp")
				|| path.contains("studentLogin.jsp") || path.contains("login")
				|| path.contains("adminLogin.jsp") || path.contains(".jpg")
				|| path.contains(".html") || path.contains(".png")
				|| path.contains(".css") || path.contains(".js")) {
			chain.doFilter(servletRequest, servletResponse);
			return;
		}
		if(currentUser == null || "".equals(currentUser)) {
			//servletResponse.sendRedirect("${pageContext.request.contextPath}/index.jsp");
			//servletRequest.getRequestDispatcher("/index.jsp").forward(request, response);
			servletResponse.sendRedirect("/BSManager/index.jsp");
		}else {
			chain.doFilter(servletRequest, servletResponse);
		}
	}

	public void destroy() {
		// TODO Auto-generated method stub
	}
}
